package com.svs.quartz.controller;

import java.sql.Connection;
import java.sql.Driver;
import java.text.MessageFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.svs.common.annotation.Log;
import com.svs.common.base.AjaxResult;
import com.svs.common.enums.BusinessType;
import com.svs.common.utils.ExcelUtil;
import com.svs.framework.shiro.util.ShiroUtils;
import com.svs.framework.web.BaseController;
import com.svs.framework.web.page.TableDataInfo;
import com.svs.quartz.domain.WisDataSource;
import com.svs.quartz.service.WisDataSourceService;

/**
 * 数据源 信息操作处理
 * 
 * @author 杜建桥
 * @date 2019-02-15
 */
@Controller
@RequestMapping("/quartz/wisDataSource")
public class WisDataSourceController extends BaseController {

	private String prefix = "quartz/wisDataSource";

	@Autowired
	private WisDataSourceService wisDataSourceService;

	@RequiresPermissions("quartz:wisDataSource:view")
	@GetMapping()
	public String wisDataSource() {
		return prefix + "/wisDataSourceList";
	}

	/**
	 * 查询数据源列表
	 */
	@RequiresPermissions("quartz:wisDataSource:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(WisDataSource wisDataSource) {
		startPage();
		List<WisDataSource> list = wisDataSourceService.selectAllList(wisDataSource);
		return getDataTable(list);
	}

	/**
	 * 导出数据源
	 * 
	 * @param wisDataSource
	 * @return
	 * @throws Exception
	 */
	@Log(title = "数据源信息导出", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(WisDataSource wisDataSource) throws Exception {
		try {
			List<WisDataSource> list = wisDataSourceService.selectAllList(wisDataSource);
			ExcelUtil<WisDataSource> util = new ExcelUtil<WisDataSource>(WisDataSource.class);
			return util.exportExcel(list, "数据源");
		} catch (Exception e) {
			return error("导出Excel失败，请联系网站管理员！");
		}
	}

	/**
	 * 去导入 数据源 信息
	 * 
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/importExcel")
	public String importExcel() throws Exception {
		return prefix + "/importWisDataSource";
	}

	/**
	 * 数据源信息导入
	 * 
	 * @param file
	 */
	@RequiresPermissions("quartz:wisDataSource:add")
	@Log(title = "数据源", businessType = BusinessType.IMPORT)
	@PostMapping("/importExcel")
	@ResponseBody
	public AjaxResult importExcelPost(@RequestParam("file") MultipartFile file) {
		try {
			ExcelUtil<WisDataSource> excel = new ExcelUtil<WisDataSource>(WisDataSource.class);
			List<WisDataSource> results = excel.importExcel(file.getInputStream());
			int len = null != results ? results.size() : 0;
			for (int i = 0; i < len; i++) {
				WisDataSource entity = results.get(i);
				wisDataSourceService.save(entity);
			}
			return success();
		} catch (Exception e) {
			return error();
		}
	}

	/**
	 * 新增数据源
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/addWisDataSource";
	}

	/**
	 * 新增保存数据源
	 */
	@RequiresPermissions("quartz:wisDataSource:add")
	@Log(title = "数据源", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(WisDataSource wisDataSource) {
		wisDataSource.setCreateTime(new Date());
		wisDataSource.setCreateBy(ShiroUtils.getLoginName());
		return toAjax(wisDataSourceService.save(wisDataSource));
	}

	/**
	 * 修改数据源
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap) {
		WisDataSource wisDataSource = wisDataSourceService.selectByPrimaryKey(id);
		mmap.put("wisDataSource", wisDataSource);
		return prefix + "/editWisDataSource";
	}

	/**
	 * 修改保存数据源
	 */
	@RequiresPermissions("quartz:wisDataSource:edit")
	@Log(title = "数据源", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(WisDataSource wisDataSource) {
		wisDataSource.setUpdateTime(new Date());
		wisDataSource.setUpdateBy(ShiroUtils.getLoginName());
		return toAjax(wisDataSourceService.update(wisDataSource));
	}

	/**
	 * 删除数据源
	 */
	@RequiresPermissions("quartz:wisDataSource:remove")
	@Log(title = "数据源", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String[] ids) {
		return toAjax(wisDataSourceService.deleteByPrimaryKeys(ids));
	}

	
	/**
	 * 修改保存数据源
	 */
	@Log(title = "测试数据源", businessType = BusinessType.UPDATE)
	@PostMapping("/testJdbc")
	@ResponseBody
	public AjaxResult testJdbc(WisDataSource wisDataSource) {
		try {
			Driver driver = (Driver) Class.forName(wisDataSource.getDialectEnum().getDriver()).newInstance();
			String jdbcUrl = wisDataSource.getDialectEnum().urlFormat() ;
			jdbcUrl = MessageFormat.format(jdbcUrl, wisDataSource.getHost(),wisDataSource.getPort()+"",wisDataSource.getDbName()) ;
			Properties info = new Properties();
	        info.put("user", wisDataSource.getUsername());
	        info.put("password", wisDataSource.getPassword());
	        Connection connection = driver.connect(jdbcUrl,info);
	        connection.close();
	        return success() ;
		} catch (Exception e) {
		}
		return error() ;
	}
}
